#include "AL_Service.h"
#include "IO_LINK_Global_Param.h"
#include "Type_Define.h"
#include "AL_OD.h"
#include "DL_Service.h"
#include "SM.h"


/********************
     暂时排除在工程外 8.3

****************/

//AL读写的回调函数
Service_AL_Write_Cnf_TYPE Service_AL_Write_Cnf;
Service_AL_Read_Cnf_TYPE  Service_AL_Read_Cnf;
 
//此服务函数仅仅提供Data和Event，调用DL服务在AL——OD状态机的T转移函数中
void Service_AL_Read_Req(uint8_t Port,uint16_t Index,uint8_t Subindex,Service_AL_Read_Cnf_TYPE CallBackFunc){
    //保存注册的回调函数
    Service_AL_Read_Cnf=CallBackFunc;

    if(AL_OD_STATE!=AL_OnReq_Idle_0){//替代了T8 T12对状态冲突检查
       Service_AL_Read_Cnf(Port,NULL,0,0x8021); //ErrorInfo = SERV_NOTAVAIL_LOCCTRL
       return;
    }else
    { 
       AL_Dirction=AL_R;  
       AL_PAGE_STRUCT.Data_Pointer=0;

       Event_AL_Service(Port); //AL_OD状态转移 
       if(Index<=1){ //Page通道  direct Param 0-15 ，对应于subindex 1-16
          if(Subindex==0){ //读取整段参数
            //用于调用DL_Read
             AL_PAGE_STRUCT.DirectAddress=Index*16+Subindex;
              
             AL_PAGE_STRUCT.DataLen=16;
              //用于AL状态机控制
             AL_ISDU_STRUCT.Index=Index;
             AL_ISDU_STRUCT.Subindex=Subindex;
            //   ISDU_Flag=None_Flag; 
            // Event_AL_Read();
             
         }else{ //读取单个参数
              
              AL_PAGE_STRUCT.DirectAddress=Index*16+Subindex-1; //addr=
              AL_PAGE_STRUCT.DataLen=1;
             //用于AL状态机控制
              AL_ISDU_STRUCT.Index=Index;
              AL_ISDU_STRUCT.Subindex=Subindex;
              //ISDU_Flag=None_Flag;
             // Event_AL_Read();
         }
         Service_AL_Read_Cnf(Port,&PAGE_BufferIn[Port],1,0x0000);
       }
       else{ //ISDU通道
              
             AL_ISDU_STRUCT.Index=Index;//用于AL状态机控制
             AL_ISDU_STRUCT.Subindex=Subindex;             
            // ISDU_Flag=Y_ISDU_Flag;  
           //  Event_AL_Read();
                      
          Service_AL_Read_Cnf(Port,AL_ReadData,AL_ReadDataLen,0x0000);
       }
    }
        

}


void Service_AL_Write_Req(uint8_t Port,uint16_t Index,uint8_t Subindex,uint8_t * Data, uint8_t DataLen,Service_AL_Write_Cnf_TYPE CallBackFunc){
    Service_AL_Write_Cnf=CallBackFunc;
    if(AL_OD_STATE!=AL_OnReq_Idle_0){  //替代了T8 T12对状态冲突检查
       Service_AL_Write_Cnf(Port,0x8021);//ErrorInfo = SERV_NOTAVAIL_LOCCTRL
       return;
    }else
    {   
        AL_Dirction=AL_W;
        AL_PAGE_STRUCT.Data_Pointer=0;
        Event_AL_Service(Port); //AL_OD状态转移 
       if(Index==0){ //Page通道  direct Param 0-15 ，对应于subindex 1-16
          
            if(Subindex==16){
            

                AL_PAGE_STRUCT.Data=Data;
                AL_PAGE_STRUCT.DirectAddress=Index*16+Subindex-1; //addr=15
                AL_PAGE_STRUCT.DataLen=1;
                 //用于AL状态机控制
                AL_ISDU_STRUCT.Index=Index;
                 AL_ISDU_STRUCT.Subindex=Subindex;
                Event_AL_Write();
            }else{
                Service_AL_Write_Cnf(Port,0x8023);// 返回ErrorInfo  IDX_NOT_ACCESSIBLE
            }
            
       }
       else if(Index==1){ //Page通道  direct Param 16-31 ，对应于subindex 1-16
         if(Subindex==0){//写整段
                 AL_PAGE_STRUCT.Data=Data;
                 for(uint8_t i=0;i<16;i++){
                     AL_PAGE_STRUCT.Data[i]=Data[i];
                 }
                AL_PAGE_STRUCT.DirectAddress=Index*16+Subindex;
                AL_PAGE_STRUCT.DataLen=16;
              
             //用于AL状态机控制
                AL_ISDU_STRUCT.Index=Index;
                 AL_ISDU_STRUCT.Subindex=Subindex;
             //ISDU_Flag=None_Flag;
             Event_AL_Write();
         }else{ //写单个参数
              AL_PAGE_STRUCT.Data[0]=Data[0];
              AL_PAGE_STRUCT.DirectAddress=Index*16+Subindex-1;
              AL_PAGE_STRUCT.DataLen=1;
             //用于AL状态机控制
              AL_ISDU_STRUCT.Subindex=Subindex;
              AL_ISDU_STRUCT.Index=1;
              //ISDU_Flag=None_Flag;
              Event_AL_Write();
         }
       }
       else{ //ISDU通道
               
             AL_ISDU_STRUCT.Index=Index;
             AL_ISDU_STRUCT.Subindex=Subindex;
             AL_ISDU_STRUCT.Data=Data;
             AL_ISDU_STRUCT.DataLen=DataLen;
            // ISDU_Flag=Y_ISDU_Flag;
             Event_AL_Write();           
            
       }
    }
        


}






